package com.ifast.asset.util;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.asset.domain.ChangerecordDO;
import com.ifast.asset.domain.SegmentDO;
import com.ifast.asset.domain.TaskipDO;
import com.ifast.asset.domain.TaskrecordDO;
import com.ifast.asset.service.ChangerecordService;
import com.ifast.asset.service.SegmentService;
import com.ifast.asset.service.TaskipService;
import com.ifast.asset.service.TaskrecordService;
import com.ifast.common.config.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class AssetUtil {

    @Autowired
    ChangerecordService changerecordService;
    @Autowired
    TaskrecordService taskrecordService;
    @Autowired
    TaskipService taskipService;
    @Autowired
    SegmentService segmentService;


    //检测两个资产信息是否发生变化
    public void getChange(TaskipDO newTaskipDO, TaskipDO oldTaskipDO) {
        System.out.println("启动记录变更线程");

        //验证操作系统是否发生改变

        if (newTaskipDO == null || oldTaskipDO == null) {
            //如果存在空 则征明 第一次扫描到 跳出
            return;
        }
        List<String> newService = JsonUtil.parseService(newTaskipDO.getPort());
        List<String> oldService = JsonUtil.parseService(oldTaskipDO.getPort());

        if (!newTaskipDO.getOs().equals(oldTaskipDO.getOs())) {

            //操作系统发生变化
            ChangerecordDO changerecordDO = new ChangerecordDO();
            changerecordDO.setIp(newTaskipDO.getIp());
            changerecordDO.setTime(newTaskipDO.getTime());
            changerecordDO.setMsg("操作系统由" + oldTaskipDO.getOs() + "变更为" + newTaskipDO.getOs());
            changerecordService.insert(changerecordDO);
        }

        for (int i = 0; i < newService.size(); i++) {

            //查看服务信息是否发生变化
            if (!oldService.contains(newService.get(i))) {

                //新增服务
                ChangerecordDO changerecordDO = new ChangerecordDO();
                changerecordDO.setIp(newTaskipDO.getIp());
                changerecordDO.setTime(newTaskipDO.getTime());
                changerecordDO.setMsg("新增" + newService.get(i) + "服务");
                changerecordService.insert(changerecordDO);
            }
        }
        for (int i = 0; i < oldService.size(); i++) {

            //查看服务信息是否发生变化
            if (!newService.contains(oldService.get(i))) {

                //减少服务
                ChangerecordDO changerecordDO = new ChangerecordDO();
                changerecordDO.setIp(newTaskipDO.getIp());
                changerecordDO.setTime(newTaskipDO.getTime());
                changerecordDO.setMsg("减少" + oldService.get(i) + "服务");
                changerecordService.insert(changerecordDO);
            }
        }

    }

    //启动线程验证   检测变化
    public void openAndDown(TaskipDO newTaskipDO, int id) {


        //ip段范围   此次任务覆盖 需要检测的ip   接下来查看 上次巡查此ip是否  开机
        Wrapper taskipwrapper = new EntityWrapper();
        taskipwrapper.eq("ip", newTaskipDO.getIp()).orderBy("time", false).notIn("id", newTaskipDO.getId());
        TaskipDO taskipDO = taskipService.selectOne(taskipwrapper);

        if (taskipDO != null && taskipDO.getStatus() == 1) {
            //验证 上次开机的情况下  服务是否发生变更
            getChange(newTaskipDO, taskipDO);
            return;
        } else {
            //进入覆盖  次ip的网段之后 没有匹配的  次ip  则征明为新启动
            //循环完 全部数据 没有此ip的开机记录   则记录此次  变更为 服务器新开机
            Wrapper wrapper = new EntityWrapper();
            wrapper.eq("ip", newTaskipDO.getIp());
            SegmentDO segmentDO = segmentService.selectOne(wrapper);
            if (segmentDO != null && "0".equals(segmentDO.getStatus())) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String time = simpleDateFormat.format(new Date());
                ChangerecordDO changerecordDO = new ChangerecordDO();
                changerecordDO.setMsg("开启设备");
                changerecordDO.setIp(newTaskipDO.getIp());
                changerecordDO.setTime(time);
                changerecordService.insert(changerecordDO);
            }

        }

    }

}
